package com.shengruan.index.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.jdbc.AuctionHistoryJdbc;
import com.shengruan.admin.jdbc.ProductClassJdbc;
import com.shengruan.admin.jdbc.ProductJdbc;
import com.shengruan.admin.vo.AuctionHistory;
import com.shengruan.admin.vo.Product;
import com.shengruan.admin.vo.ProductClass;
import com.shengruan.admin.vo.User;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.framework.pager.Pager;
import com.shengruan.user.action.AutoAuctionUtil;

public class ProductAction extends BaseAction {

    Logger logger = LoggerFactory.getLogger(getClass());

    private ProductJdbc productJdbc = (ProductJdbc)this.getJdbc(ProductJdbc.class);
    private ProductClassJdbc productClassJdbc = (ProductClassJdbc)this.getJdbc(ProductClassJdbc.class);
    private AuctionHistoryJdbc auctionHistoryJdbc = (AuctionHistoryJdbc)this.getJdbc(AuctionHistoryJdbc.class);

    public void list() throws Exception {
        String classId = this.input("classId");

        if (StringUtils.isNotEmpty(classId)) {
            if("All".equalsIgnoreCase(classId)) classId = "";
        }

        int currentPage = 1;
        try {currentPage = Integer.parseInt(this.input("currentPage"));} catch (Exception ex) {}

        Pager pager = new Pager(currentPage, productJdbc.getRows(null, classId, String.valueOf(Constants.STATUS_ACTIVE), null), 9);
        List<Product> productList = productJdbc.findWithPage(currentPage, 9, null, classId, String.valueOf(Constants.STATUS_ACTIVE), null);

        List<ProductClass> productClassList = this.productClassJdbc.getList();

        this.setRequest("classId", classId);
        this.setRequest("pager", pager);
        this.setRequest("productList", productList);
        this.setRequest("productClassList", productClassList);

        this.forward("/index.jsp");
    }

    /**
     * 加载商品详细信息
     * @return
     * @throws Exception
     */
    public void details() throws Exception {
        String id = this.input("id");
        if (StringUtils.isEmpty(id)) {
            this.redirect("/index.html");
            return;
        }
      
        Product product = productJdbc.getById(Integer.valueOf(id));
        if(product==null){
        	 this.redirect("/index.html");
             return;
        }

        if(product.getImagePath()==null){
        	product.setImagePath("");
        }

        this.setRequest("product", product);
        this.forward("/product.jsp");
    }
}
